package org.arrow.tap.components;

import java.util.HashMap;

import org.apache.tapestry5.Block;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.SessionState;

public class IfRole {
    /** *//**
     * A comma-separated list of roles is supplied to one or more of the
     * following parameters. If none are supplied, the default behavior is to
     * forbid access. Behavior should be self-explanatory.
     */
    @Parameter(required = false, defaultPrefix = "literal")
    private String ifAllGranted;

    @Parameter(required = false, defaultPrefix = "literal")
    private String ifAnyGranted;

    @Parameter(required = false, defaultPrefix = "literal")
    private String ifNotGranted;

    /** *//**
     * An alternate {@link Block} to render if the test parameter is false. The default, null, means
     * render nothing in that situation.
     */
    @Parameter(name = "else")
    private Block elseBlock;

    private boolean test;
   
    @SessionState(create=false)
    private HashMap nowUser;
    
    private boolean nowUserExists;
//    private AuthenticationService auth;

    private boolean checkPermission() {
    	return nowUserExists;
//        return auth.checkPermission(ifNotGranted, ifAllGranted, ifAnyGranted);
    }
   
    void setupRender() {
        test = checkPermission();
    }

    /** *//**
     * Returns null if the test method returns true, which allows normal
     * rendering (of the body). If the test parameter is false, returns the else
     * parameter (this may also be null).
     */
    Object beginRender() {
        return test ? null : elseBlock;
    }

    /** *//**
     * If the test method returns true, then the body is rendered, otherwise not. The component does
     * not have a template or do any other rendering besides its body.
     */
    boolean beforeRenderBody() {
        return test;
    }
   
}


